
// 自动退出登录
function checkActive() {
    let lastActiveTime = localStorage.getItem('lastActiveTime')
    let now = new Date().getTime()
    if (!lastActiveTime || now - lastActiveTime > 1000 * 60 * 60) {
        if (lastActiveTime) {
            alert('登录已过期')
        }
        window.location.href = './login.html'
    }
}

// 获取格式化时间字符串
function getFormatTime(date) {
    date = new Date(date)
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let minute = date.getMinutes()
    let second = date.getSeconds()

    // 补零
    month = month < 10 ? '0' + month : month;
    day = day < 10 ? '0' + day : day;
    hour = hour < 10 ? '0' + hour : hour;
    minute = minute < 10 ? '0' + minute : minute;
    second = second < 10 ? '0' + second : second;
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}


// 验证用户名, 5~16位
function validateUsername(username) {
    if (username === '') {
        return '用户名不能为空'
    }
    if (username.length < 5 || username.length > 16) {
        return '用户名长度应为5~16位'
    }
    return '';
}

// 验证密码, 5到16位
function validatePassword(password) {
    if (password === '') {
        return '密码不能为空'
    }
    if (password.length < 5 || password.length > 16) {
        return '密码长度应为5~16位'
    }
    return '';
}

// 验证邮箱
function validateEmail(email) {
    if (email === '') {
        return '邮箱不能为空'
    }

    var reg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (reg.test(email)) {
        return ''
    }
    else {
        return '请输入正确的邮箱'
    }
}